const { getAllForumList, getForumList, getForumOfId, getForumComment, getForumCommentPag, publishComment, releaseForum } = require("../model/forumModel")
module.exports = {
  // 获取论坛数据
  getForumList: async (ctx) => {
    // 默认值
    let current = 1;
    let pageSize = 10;
    // 如果传了current参数就重新赋值
    if (ctx.request.query.current) {
      current = parseInt(ctx.request.query.current)
    }
    // 如果传了pageSize参数就重新赋值
    if (ctx.request.query.pageSize) {
      pageSize = parseInt(ctx.request.query.pageSize)
    }

    let all = await getAllForumList();
    let res = await getForumList(current, pageSize);

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res,
        total: all.length
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无数据",
        data: [],
        total: 0,
      }
    }
  },
  // 通过id获取论坛
  getForumOfId: async (ctx) => {
    const { posts_id } = ctx.request.query;
    // 通过id获取论坛
    let res = await getForumOfId(posts_id);

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res[0],
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无数据",
      }
    }
  },
  // 获取论坛评论列表
  getForumComment: async (ctx) => {
    const { posts_id } = ctx.request.query;
    // 默认值
    let current = 1;
    let pageSize = 10;
    // 如果传了current参数就重新赋值
    if (ctx.request.query.current) {
      current = parseInt(ctx.request.query.current)
    }
    // 如果传了pageSize参数就重新赋值
    if (ctx.request.query.pageSize) {
      pageSize = parseInt(ctx.request.query.pageSize)
    }


    let all = await getForumComment(posts_id);
    let res = await getForumCommentPag(posts_id, current, pageSize);

    if (all.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res,
        total: all.length
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无数据",
        data: [],
        total: 0,
      }
    }
  },
  // 发表评论
  publishComment: async (ctx) => {
    const { posts_id, user_id, user_name, user_avatar, content, createtime } = ctx.request.body;
    // 发表评论
    let res = await publishComment(posts_id, user_id, user_name, user_avatar, content, createtime)

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "发表成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "发表失败"
      }
    }
  },
  // 发布论坛
  releaseForum: async (ctx) => {
    const { user_id, user_name, user_avatar, title, details_content, createtime } = ctx.request.body;
    let brief_content = details_content.slice(0, 199);
    // 发布论坛
    let res = await releaseForum(user_id, user_name, user_avatar, title, details_content, brief_content, createtime);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "发布成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "发布失败"
      }
    }
  }
}